﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GamePlugin.Logics.Core
{
    using System;
    using System.Diagnostics;

    public class Time
    {
        private static DateTime fpsStartTime;

        private static int frameCount;

        public static TimeSpan DeltaTime { get; set; }

        public static float Fps { get; set; }

        public static TimeSpan TotalTime { get; set; }

        private static DateTime LastFrameTime { get; set; }

        private static DateTime StartTime { get; set; }

        public static void Initialize()
        {
            StartTime = DateTime.Now;
            LastFrameTime = StartTime;
            DeltaTime = new TimeSpan(0, 0, 0);
            TotalTime = new TimeSpan(0, 0, 0);

            fpsStartTime = DateTime.Now;
            frameCount = 0;
        }

        public static void Tick()
        {
            // update time
            DateTime now = DateTime.Now;

            DeltaTime = new TimeSpan(now.Ticks - LastFrameTime.Ticks);
            TotalTime = new TimeSpan(now.Ticks - StartTime.Ticks);

            LastFrameTime = now;

            // update frame
            frameCount++;
            if (frameCount >= 30)
            {
                Fps = frameCount / (float)(new TimeSpan(now.Ticks - fpsStartTime.Ticks)).TotalSeconds;
                fpsStartTime = DateTime.Now;
                frameCount = 0;
            }
        }
    }
}
